package com.atguigu.serviceedu.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.client.IsBuyCourseClient;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.chapter.ChapterVo;
import com.atguigu.serviceedu.entity.frontVo.CourseFrontVo;
import com.atguigu.serviceedu.entity.frontVo.CourseWebVo;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin
@RequestMapping("/eduservice/frontCourse")
public class FrontCourseController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private IsBuyCourseClient isBuyCourseClient;
    @PostMapping("getFrontCourse/{page}/{limit}")
    public R pageList(@PathVariable Long page,
                      @PathVariable Long limit,
                      @RequestBody(required = false) CourseFrontVo courseFrontVo
                      ){
        Page<EduCourse> coursePage = new Page<>(page,limit);
        Map<String,Object> map = courseService.getPageList(coursePage,courseFrontVo);
        return R.ok().data(map);
    }
    //根据课程id获取前台课程详情信息
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id获取课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id获取章节小节信息
        List<ChapterVo> chapterAndVideo = chapterService.getChapterAndVideo(courseId);
        //根据课程id和用户id查询支付状态
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        int result = isBuyCourseClient.isBuyCourse(courseId, memberId);
        return R.ok().data("courseWebVo",courseWebVo).data("chapterAndVideo",chapterAndVideo).data("result",result);
    }
    //根据课程id获取课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public com.atguigu.commonutils.vo.CourseWebVo getCourseInfoOrder(@PathVariable String id){
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(id);
        com.atguigu.commonutils.vo.CourseWebVo courseWebVoOrder = new com.atguigu.commonutils.vo.CourseWebVo();
        BeanUtils.copyProperties(baseCourseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }

}
